package com.bysj.controller;

import com.bysj.entity.Goods;
import com.bysj.entity.Topic;
import com.bysj.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    //添加商品
    @PostMapping("/addGood")
    public String addGoods(@RequestBody Goods goods){
        goodsService.addGoods(goods);
        return "ok";
    }

    //查询所有商品
    @RequestMapping("/getGoodsAll")
    @CrossOrigin
    public List<Goods> getAllGoods(Map map){
        return goodsService.getAllGoods(map);
    }

    //根据id查询商品
    @RequestMapping("/getGoodsById")
    @CrossOrigin
    public List<Goods> getGoodsById(Integer id){
        return goodsService.getGoodsById(id);

    }
    @RequestMapping("/getGoodsByUid")
    //根据用户id查询商品
    public List<Goods> getGoodsByUid(Integer id){
        return goodsService.getGoodsByUid(id);
    }

    //根据商品id删除商品
    @ResponseBody
    @RequestMapping("/deleteGoodsById")
    @CrossOrigin
    public String deleteGoodsById(Integer id){
        goodsService.deleteGoodsById(id);
        return "success";
    }
    //根据id修改商品
    @ResponseBody
    @RequestMapping("/updateGoodsById")
    public String updateGoodsById(@RequestBody Goods goods){
        goodsService.updateGoodsById(goods);
        return "success";
    }

    //根据输入的文字查询相关商品
    @RequestMapping("/getGoodsByWords")
    @CrossOrigin
    public List<Goods> getGoodsByWords( String searchwords){
        return goodsService.getGoodsByWords(searchwords);
    }

    //分页查询所有商品
    @GetMapping("/getAllGoodsByPage")
    @CrossOrigin
    public PageInfo<Goods> getAllGoodsByPage(Map map,
                                             @RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam(defaultValue = "10") int pageSize ){
        PageInfo<Goods> goodsList = goodsService.getAllGoodsByPage(map,pageNum,pageSize);
        return goodsList;


    }


}
